package com.bckj.freshnow.order.service;

import com.alibaba.cola.extension.BizScenario;
import com.alibaba.cola.extension.ExtensionExecutor;
import com.bckj.freshnow.common.constant.TradeOrderConstants;
import com.bckj.freshnow.order.entity.TradeOrder;
import com.bckj.freshnow.order.mapper.TradeOrderMapper;
import com.bckj.freshnow.order.model.CreateTradeOrderRequest;
import com.bckj.freshnow.order.model.CreateTradeOrderResponse;
import com.bckj.freshnow.order.repo.TradeOrderRepository;
import com.bckj.freshnow.order.convert.TradeOrderConvert;
import com.bckj.freshnow.order.model.TradeOrderResponse;
import com.bckj.freshnow.order.model.TradeOrderRequest;

import com.bckj.freshnow.order.trade.extension.CreateTradeOrderExtPt;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 交易订单表(TradeOrder)service
 *
 * @author makejava
 * @since 2025-04-22 10:19:42
 */
@Service
@RequiredArgsConstructor
public class TradeOrderService {

    private final TradeOrderRepository tradeOrderRepository;
    private final TradeOrderMapper tradeOrderMapper;
    private final TradeOrderConvert tradeOrderConvert;
    private final ExtensionExecutor extensionExecutor;
	
    /**
     * 列表
     *
     * @param request req
     * @return list
     */
    public List<TradeOrderResponse> findAll(TradeOrderRequest request){
        return tradeOrderMapper.findAll(request);
    }
    
    /**
     * 详情
     *
     * @param id id
     * @return vo
     */
    public TradeOrderResponse details(Long id) {
        TradeOrder entity = tradeOrderRepository.getById(id);
        return tradeOrderConvert.bean2Response(entity);
    }
    
    /**
	 * 增加
	 *
	 * @param request req
	 * @return boolean
	 */
    public boolean save(TradeOrderRequest request) {
        TradeOrder data = tradeOrderConvert.request2Bean(request);
        return tradeOrderRepository.save(data);
    }
    
    /**
	 * 修改
	 *
	 * @param request req
	 * @return boolean
	 */
	public boolean update(TradeOrderRequest request) {
        TradeOrder data = tradeOrderConvert.request2Bean(request);
        return tradeOrderRepository.updateById(data);
	}

	/**
	 * 根据Id移除
	 *
	 * @param id id
	 * @return boolean
	 */
	public boolean remove(Long id) {
		return tradeOrderRepository.removeById(id);
	}


    public CreateTradeOrderResponse preCreate(CreateTradeOrderRequest request) {
        return extensionExecutor.execute(CreateTradeOrderExtPt.class, getBizScenarioByRequest(request), ext -> ext.preCreate(request));
    }

    public CreateTradeOrderResponse pricing(CreateTradeOrderRequest request) {
        return extensionExecutor.execute(CreateTradeOrderExtPt.class, getBizScenarioByRequest(request), ext -> ext.pricing(request));
    }

    public CreateTradeOrderResponse create(CreateTradeOrderRequest request) {
        return extensionExecutor.execute(CreateTradeOrderExtPt.class, getBizScenarioByRequest(request), ext -> ext.create(request));
    }

    private BizScenario getBizScenarioByRequest(CreateTradeOrderRequest request) {

        //默认走自定义业务
        return BizScenario.valueOf(TradeOrderConstants.BIZ_ID_CUSTOM);
    }
}

